package com.teach.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.teach.dto.PhotoPicSearch;
import com.teach.dto.PhotoPopularAuthorDTO;
import com.teach.entity.PhotoPic;

public interface PhotoPicMapper {
    int deleteByPrimaryKey(Long photoPicId);

    int insert(PhotoPic record);

    int insertSelective(PhotoPic record);

    PhotoPic selectByPrimaryKey(Long photoPicId);

    int updateByPrimaryKeySelective(PhotoPic record);

    int updateByPrimaryKey(PhotoPic record);
    
    @Select("select * from photo_pic limit #{index},#{len}")
    List<PhotoPic> selectAll(@Param("index")int index,
    		@Param("len")int len);
    
    @Select("SELECT * from photo_pic ORDER BY photo_pic_great_count DESC LIMIT 0,6")
    List<PhotoPic> selectGreatDESC();
    
    @Select("select * from photo_pic where photo_writer_id = #{photoWriterId} ORDER BY photo_release_time desc, photo_pic_id asc")
    List<PhotoPic> selectByWriterId(@Param("photoWriterId")String photoWriterId);
 
    @Select("SELECT * FROM photo_pic "
    		+ "WHERE photo_pic_title = #{photoPicTitle} "
    		+ "AND photo_writer_id = #{photoWriterId} "
    		+ "AND photo_pic_class_id=#{photoPicClassId}")
    List<PhotoPic> selectPicDetails(@Param("photoPicTitle")String title,
    								@Param("photoWriterId")String writerId,
    								@Param("photoPicClassId")Long picClassId);
    
    /*
     * 查询作品点赞数
     */
    @Select("select photo_pic_great_count from photo_pic where photo_pic_title = #{photoPicTitle} ORDER BY photo_pic_great_count DESC LIMIT 0,1")
    Long selectPicTitle(@Param("photoPicTitle")String photoPicTitle);
    
    /**
     * 当月作品
     * @param photoReleaseTime
     * @param userName
     * @return
     */
    @Select("SELECT	user_info.*, sum(photo_pic.photo_pic_great_count) as photoPicGreatCount FROM user_info INNER JOIN photo_pic on user_info.USER_NAME = photo_pic.photo_writer_id WHERE photo_release_time LIKE #{photoReleaseTime} and USER_NAME = #{userName}")
    PhotoPopularAuthorDTO selectPopularAuthor(@Param("photoReleaseTime")String photoReleaseTime,@Param("userName") String userName);
    /**
     *按作者查询当月作品数
     * @param photoReleaseTime
     * @param userName
     * @return
     */
    @Select("SELECT COUNT(*) FROM user_info INNER JOIN photo_pic on user_info.USER_NAME = photo_pic.photo_writer_id WHERE photo_release_time LIKE #{photoReleaseTime} and USER_NAME = #{userName}")
    Integer selectWorksCount(@Param("photoReleaseTime")String photoReleaseTime,@Param("userName") String userName);
    
    @Select("SELECT * FROM photo_pic "
    		+ "WHERE photo_pic_title = #{photoPicTitle} "
    		+ "AND photo_writer_id = #{photoWriterId} "
    		+ "AND photo_pic_class_id=#{photoPicClassId} order by photo_pic_great_count desc limit 0,1")
    PhotoPic selectPicDetailsDESC(@Param("photoPicTitle")String title,
    							   @Param("photoWriterId")String writerId,
    								@Param("photoPicClassId")Long picClassId);
    
    @Select("select * from photo_pic where photo_pic_class_id = #{picClassId}")
    List<PhotoPic> selectType(
    		@Param("picClassId")Long picClassId);
    
    /**
     * 分页模糊查询
     * @param search
     * @return
     */
    List<PhotoPic> pageSearch(PhotoPicSearch search);
    
    /**
     * 作者信息+获赞数+作品数
     * @param userName
     * @return
     */
    @Select("SELECT	user_info.*, sum(photo_pic.photo_pic_great_count) as photoPicGreatCount,count(*) AS picCount FROM user_info INNER JOIN photo_pic on user_info.USER_NAME = photo_pic.photo_writer_id WHERE USER_NAME = #{userName}")
    PhotoPopularAuthorDTO selectAuthorInfo(@Param("userName")String userName);
}
